var pageNum = null;
var pageSize = 10;
let vm = new Vue({
    el: '.main-content',
    data: function () {
        return {
            pageInfo: {},
            name: "全部",
            password2: '',
            id: '',
            sysUser: {
                officeId: '0'
            },
            active: true,
            condition: {
                name: '',
                roles: '',
            },
            setting: {
                data: {
                    simpleData: {
                        enable: true,
                        idKey: 'id',
                        pIdKey: 'parentId',
                    },
                },
                callback: {
                    onClick: this.onClick
                },
                view: {
                    fontCss: this.fontCss
                }
            },

            nodes: []
        }
    },
    methods: {

        getPage: function (pageNum, pageSize) {
            selectPage(pageNum, pageSize)
        },

        selectPage: function (pageNum = 1, pageSize = 10) {
            axios({
                url: `/guguanjia/sysuser/selectPage/${pageNum}/${pageSize}`,
                params: this.condition
            }).then(response => {
                vm.$data.pageInfo = response.data;
            }).catch(error => {
                console.log(error.message)
            })
        },


        selectAll: function () {
            //查询全部：  1.清空查询条件对象值  2.调用selectPage
            this.condition = {
                id: '',
                name: '',
                roles: ''
            };
            selectPage(pageNum, pageSize)
        },

        //添加
        insert: function () {
            if (this.sysUser.password == this.password2) {
                axios({
                    url: "/guguanjia/sysuser/insert",
                    method: "POST",
                    data: this.sysUser
                }).then(response => {
                    //清空
                    this.sysUser = {};
                    this.sysUser.officeId= '0';
                    this.password2 = '';
                    this.name= "全部";
                    //切换
                    this.active = !this.active;
                    layer.msg("添加成功");
                }).catch(error => {
                    // layer.msg("请完成填写")
                    layer.msg(error.message)
                })
            }else{
                layer.msg("密码不一致")
            }
        },
        aa: function () {
            this.active = !this.active
        },
        //删除
        doDelete: function (id) {
            layer.msg('是否删除', {
                time: 20000, //20s后自动关闭
                btn: ['是', '否'],
                yes: () => {
                    //删除标记（0：正常；1：删除；2：审核；）
                    id.delFlag = "1";
                    axios({
                        url: "/guguanjia/sysuser/updateById",
                        method: "POST",
                        data: id,
                    }).then(response => {
                        layer.msg("删除成功")
                        this.getPage(pageNum, pageSize)
                        // this.selectPage();//刷新页面
                    }).catch(error => {
                        layer.msg(error.message)
                    });
                }
            });
        },
        //修改
        toUpdate: function (id) {
            layer.open({
                type: 2,
                skin: 'layui-layer-demo', //样式类名
                title: '编辑',
                closeBtn: 0, //不显示关闭按钮
                anim: 2,
                area: ['80%', '80%'],
                shadeClose: true, //开启遮罩关闭
                content: '/guguanjia/sysuser/toUpdate',
                success: function (aa, index) {
                    var iframe = window['layui-layer-iframe' + index];
                    iframe.abcdefg(id)
                },
                end: function () {
                    selectPage(pageNum, pageSize);
                }
            })

        },

        initTree: function () {
            //初始化nodes数据
            axios({
                url: "/guguanjia/office/findAll"
            }).then(response => {
                this.nodes = response.data;
                this.nodes[this.nodes.length] = {"id": 0, "name": "全部"};
                $.fn.zTree.init($("#pullDownTreeone"), this.setting, this.nodes)
            }).catch(error => {
                layer.msg(error.message)
            })

        },
        initTreetwo: function () {
            //初始化nodes数据
            axios({
                url: "/guguanjia/office/findAll"
            }).then(response => {
                this.nodes = response.data;
                this.nodes[this.nodes.length] = {"id": 0, "name": "全部"};
                $.fn.zTree.init($("#pullDownTreetwo"), this.setting, this.nodes)
            }).catch(error => {
                layer.msg(error.message)
            })

        },
        onClick: function (event, treeld, treeNode) {
            this.name = treeNode.name;
            this.sysUser.officeId = treeNode.id;
            console.log(this.sysUser.officeId = treeNode.id)
            if (treeNode.id === 0) {
                //如果选择的是全部
                this.condition.id = null;
            } else {
                //绑定查询条件
                this.condition.id = treeNode.id;
            }

            //删除open
            $(".btn-group").removeClass("open");
        },
        search: function () {
            //根据树id获取树对象
            let treeObj = $.fn.zTree.getZTreeObj("pullDownTreeone");

            //初始化树节点，防止之前设置的high届性影响
            //transformToArray :将多维结构节点数组，转换成简单数纽―维结构
            let arrayNodes = treeObj.transformToArray(treeObj.getNodes());
            for (let i = 0; i < arrayNodes.length; i++) {
                arrayNodes[i].hightLight = false;
                //更新节点，触发调用 fontCss 方法
                treeObj.updateNode(arrayNodes[i])
            }


            //getNodesByParamFuzzy ( key , value, parentNode ) :根据key指定的届性，value指定的值
            // 从指定的parentNode节点中模糊查找parentNode为null则全部查找
            let nodesFuzzy = treeObj.getNodesByParamFuzzy("name", this.name, null);

            //将所有查找到的元素设置为高亮
            for (let i in nodesFuzzy) {
                nodesFuzzy[i].hightLight = true;

                //更新节点，触发调用 fontCss 方法
                treeObj.updateNode(nodesFuzzy[i])
            }


        },
        fontCss: function (treeId, treeNode) {
            // 根据高亮标记设置高亮红色
            return treeNode.hightLight ? {"color": "red"} : {"color": "black"}

        },


    },

    created: function () {
        selectPage(1, 10);
        this.initTree()
        this.initTreetwo()
    }

});

function selectPage(pageNum, pageSize) {
    this.pageNum = pageNum;
    this.pageSize = pageSize;
    axios({
        url: `/guguanjia/sysuser/selectPage/${pageNum}/${pageSize}`,
    }).then(response => {
        vm.$data.pageInfo = response.data;
    }).catch(error => {
        console.log(error.message)
    })
}